package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.UserAddr;
import com.powernode.service.UserAddrService;
import com.powernode.mapper.UserAddrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr>
        implements UserAddrService {
    @Autowired
    private UserAddrMapper userAddrMapper;

    /**
     * 新增收货地址
     * 判断使用已经存在收货地址 存在则直接添加 不存在则设为默认地址
     *
     * @param wxUserId
     * @param userAddr
     */
    @Override
    public void addAddress(String wxUserId, UserAddr userAddr) {
        //查询该用户的所有收货地址
        List<UserAddr> userAddrs = userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, wxUserId)
                .eq(UserAddr::getStatus, 1));
        userAddr.setUserId(wxUserId);
        userAddr.setStatus(1);
        userAddr.setCreateTime(new Date());
        userAddr.setUpdateTime(new Date());

        if (CollectionUtil.isNotEmpty(userAddrs)) {
            userAddrMapper.insert(userAddr);
        } else {
            //设置默认地址
            userAddr.setCommonAddr(1);
            userAddrMapper.insert(userAddr);
        }

    }

    /**
     * 更改默认地址
     *
     * @param wxUserId
     * @param addrId
     */
    @Override
    @Transactional
    public void defaultAddr(String wxUserId, Long addrId) {
        //如果存在对象 则该地址已经是默认地址了
        //由于当此按钮能够点击的时候 至少存在一个地址 所以userAddr必定存在
        UserAddr userAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, wxUserId)
                .eq(UserAddr::getStatus, 1)
                .eq(UserAddr::getCommonAddr, 1));
        userAddr.setCommonAddr(0);
        userAddr.setUpdateTime(new Date());
        userAddrMapper.updateById(userAddr);
        //获得到点击的这个地址
        UserAddr addr = userAddrMapper.selectById(addrId);
        addr.setCommonAddr(1);
        addr.setUpdateTime(new Date());
        userAddrMapper.updateById(addr);

      /*  if (ObjectUtils.isEmpty(userAddr)) {
            //如果为空 说明此地址不是默认
            //先删除之前的地址
            UserAddr addr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                    .eq(UserAddr::getUserId, wxUserId)
                    .eq(UserAddr::getCommonAddr, 1));
            addr.setCommonAddr(0);
            addr.setUpdateTime(new Date());
            userAddrMapper.updateById(addr);
            //获取到该地址
            UserAddr one = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                    .eq(UserAddr::getAddrId, addrId));
            one.setUpdateTime(new Date());
            one.setCommonAddr(1);
            userAddrMapper.updateById(one);
        } else {
            userAddr.setUpdateTime(new Date());
            userAddrMapper.updateById(userAddr);
        }*/
    }
}




